<template>
	<el-scrollbar :height="isTagsview ? `calc(100vh - 154px)` : `calc(100vh - 120px)`">
		<div>
			<zh-form ref="formRef" style="width: 96%" :md="12" :lg="12" :xl="12" :model="orgInfo" :rules="rules">
				<zh-form-input :disabled="props.org.disabled" prop="orgCode" label="组织编码" placeholder="" v-model="orgInfo.orgCode"></zh-form-input>
				<zh-form-input :disabled="props.org.disabled" prop="orgName" label="组织名称" placeholder="" v-model="orgInfo.orgName"></zh-form-input>
				<zh-form-input
					:disabled="props.org.disabled"
					prop="creditCode"
					label="社会信用码"
					placeholder=""
					v-model="orgInfo.creditCode"
				></zh-form-input>
				<zh-form-input prop="parentId" label="上级ID" placeholder="" v-model="orgInfo.parentId" disabled></zh-form-input>
				<zh-form-input prop="parentName" label="上级名称" placeholder="" v-model="orgInfo.parentName" disabled></zh-form-input>
				<zh-form-cascader
					prop="orgType"
					label="组织类型"
					placeholder="请选择"
					:show-all-levels="false"
					:props="{ label: 'title', leaf: 'isLeaf', treeList: 'treeList', emitPath: false, checkStrictly: true }"
					v-model="orgInfo.orgType"
					:options="state.orgTypeSelectTreeData"
					:disabled="props.org.disabled"
				></zh-form-cascader>
				<zh-form-input prop="parentPath" :span="24" label="所有上级" disabled placeholder="" v-model="orgInfo.parentPath"></zh-form-input>
				<zh-form-select
					prop="status"
					:disabled="props.org.disabled"
					label="状态"
					:options="state.dictData.flag"
					placeholder="请选择"
					v-model="orgInfo.status"
				></zh-form-select>
				<zh-form-select
					prop="fictitious"
					label="虚拟组织"
					:disabled="props.org.disabled"
					:options="state.dictData.flag"
					placeholder="请选择"
					v-model="orgInfo.fictitious"
				></zh-form-select>
				<zh-form-select
					prop="treeFlag"
					label="树虚拟"
					:disabled="props.org.disabled"
					:options="state.dictData.flag"
					placeholder="请选择"
					v-model="orgInfo.treeFlag"
				></zh-form-select>
				<zh-form-cascader
					prop="areaCode"
					label="行政区划"
					:props="{ checkStrictly: true, emitPath: false }"
					placeholder="请选择"
					v-model="orgInfo.areaCode"
					:disabled="props.org.disabled"
					:options="state.areaCodeSelectTreeData"
				></zh-form-cascader>
				<zh-form-select
					prop="orgPro"
					label="组织性质"
					:disabled="props.org.disabled"
					:options="state.dictData.orgPro"
					placeholder="请选择"
					v-model="orgInfo.orgPro"
				></zh-form-select>
				<zh-form-select
					placeholder="请搜索后选择主管单位"
					funcCode="org0001"
					label="主管单位"
					prop="mainDeptId"
					searchField="orgName"
					:fieldNames="{
						label: 'orgName',
						value: 'orgId',
					}"
					:lazy="true"
					:disabled="props.org.disabled"
					v-model="orgInfo.mainDeptId"
				>
				</zh-form-select>
				<zh-form-select
					prop="insideFlag"
					label="科室标识"
					:options="state.dictData.flag"
					:disabled="props.org.disabled"
					placeholder="请选择"
					v-model="orgInfo.insideFlag"
				></zh-form-select>

				<zh-form-input prop="contact" label="联系人" :disabled="props.org.disabled" placeholder="" v-model="orgInfo.contact"></zh-form-input>
				<zh-form-input prop="cellphone" label="联系手机" :disabled="props.org.disabled" placeholder="" v-model="orgInfo.cellphone"></zh-form-input>
				<zh-form-input prop="phone" label="联系电话" :disabled="props.org.disabled" placeholder="" v-model="orgInfo.phone"></zh-form-input>

				<zh-form-select
					prop="hasAccount"
					label="账号情况"
					:options="state.dictData.flag"
					placeholder="请选择"
					v-model="orgInfo.hasAccount"
					disabled
				></zh-form-select>
				<zh-form-number prop="orderNo" label="排序" placeholder="" v-model="orgInfo.orderNo" disabled></zh-form-number>
				<zh-form-number prop="rate" label="级别" placeholder="" v-model="orgInfo.rate" disabled></zh-form-number>
				<zh-form-input prop="address" label="地址" placeholder="" v-model="orgInfo.address" :disabled="props.org.disabled"></zh-form-input>
				<zh-form-input
					prop="remarks"
					:span="24"
					type="textarea"
					label="备注"
					placeholder=""
					v-model="orgInfo.remarks"
					:disabled="props.org.disabled"
				></zh-form-input>
			</zh-form>
		</div>
	</el-scrollbar>
</template>

<script lang="ts" setup name="orgForm">
const props = defineProps({
	org: {
		type: Object,
		default: () => {},
	},
});
const state = reactive({
	hideChecked: false,
	confirmLoading: false,
	dictData: {} as any,
	orgTypeSelectTreeData: [],
	areaCodeSelectTreeData: [],
	// form: state.$form.createForm(this),
	form: '',
	initData: {},
});
const { isTagsview } = storeToRefs($store.theme);
const orgInfo = ref<any>({});

const rules = reactive({
	orgCode: [
		{
			required: true,
			message: '请输入组织编码',
			trigger: 'blur',
		},
	],
	orgName: [
		{
			required: true,
			message: '请输入组织名称',
			trigger: 'blur',
		},
	],
	orgType: [
		{
			required: true,
			message: '请选择组织类型',
			trigger: 'blur',
		},
	],
	status: [
		{
			required: true,
			message: '请选择状态',
			trigger: 'blur',
		},
	],
	fictitious: [
		{
			required: true,
			message: '请选择是否虚拟组织',
			trigger: 'blur',
		},
	],
	treeFlag: [
		{
			required: true,
			message: '请选择是否树虚拟',
			trigger: 'blur',
		},
	],
});
const formRef = ref();
function getAreaCodeData() {
	setTimeout(() => {
		$api.common.send.post('dict0026', {}).then((res) => {
			state.areaCodeSelectTreeData = res.result;
		});
	}, 20);
}

function setInitDate(info) {
	formRef.value?.resetFields();
	if (info) {
		orgInfo.value = info;
	}
}
const queryOrg = () => {
	if (props.org?.orgId) {
		let params = { orgId: props.org?.orgId };
		$api.common.send.post('org0002', params).then((res) => {
			orgInfo.value = res.result || {};
		});
	}
};
//保存数据
const saveForm = async () => {
	//校验表单
	let checkFlag = await formRef.value.checkForm();
	if (checkFlag) {
		//校验通过
		let saveForm = Object.assign({}, orgInfo.value);
		if (!saveForm.orgId) {
			let res = await $api.common.send.post('org0004', saveForm);
			res.saveType = 'add';
			return res;
		} else {
			let res = await $api.common.send.post('org0005', saveForm);
			res.saveType = 'edit';
			return res;
		}
	}
	return null;
};
//重置表单
const resetForm = async () => {
	await formRef.value?.resetFields();
};

watch(
	() => props.org,
	(val: any) => {
		console.log(val);
		if (val && val.orgId) {
			queryOrg();
		} else {
			let myForm = Object.assign({}, val);
			orgInfo.value = myForm;
		}
	},
	{
		deep: true,
	}
);
// 数据初始化
const params = {
	dictFields: 'orgPro,flag',
};
$api.common.getBatchDict.post(params).then((res) => {
	state.dictData = res.result;
});
const paramsTree = {
	dictField: 'orgType',
};
$api.common.send.post('dict0025', paramsTree).then((res) => {
	state.orgTypeSelectTreeData = res.result;
});
getAreaCodeData();
defineExpose({ setInitDate, form: formRef, orgInfo, saveForm, resetForm });
</script>
